package cn.acewill.yxd.yunxundian.ui.sign;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.adapter.SignEvaAdapter;
import cn.acewill.yxd.yunxundian.componet.BaseFragment;
import cn.acewill.yxd.yunxundian.model.EvaluteBean;
import cn.acewill.yxd.yunxundian.pubClass.OkHttpManger;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;

/**
 * Created by AAA on 2017/4/7.
 */

public class FunctionEvaFragment extends BaseFragment{
    /**
     * 标志位，标志已经初始化完成
     */
    private boolean isPrepared;
    /**
     * 是否已被加载过一次，第二次就不再去请求数据了
     */
    private boolean mHasLoadedOnce = false;

    public ListView mylist;
    public int shopid;
    public String searchtype="zhineng";
    public SignEvaAdapter adapter;
    private RelativeLayout loadingView;

    /**
     * 可以只需一个fragment加载同一布局类型
     * fragment.setArguments发送position
     */
//    public static FunctionEvaFragment newInstance(int index){
//        FunctionEvaFragment fragment= new FunctionEvaFragment();
//        Bundle bundle = new Bundle();
//        bundle.putInt("index", index);
//        fragment.setArguments(bundle);
//        return fragment;
//    }
//


    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.mylistview,container,false);
        shopid= HistoryEvaluationActivity.shopid;
        this.mylist= (ListView) view.findViewById(R.id.myList);
        this.loadingView= (RelativeLayout) view.findViewById(R.id.loadingRel);
        isPrepared = true;
        lazyLoad();
        return view;
    }

    public void loadData(){
        Map<String,String> map=new HashMap<>();
        map.put("task","getSignupReportByShopidAndType");
        map.put("shopid",shopid+"");
        map.put("searchtype",searchtype);
        try {
            OkHttpManger.getInstance().postAsync(pubClass.Server_namehzm, new OkHttpManger.MyCallback() {
                @Override
                public void onSuccess(String result) {
                    String[] array = result.split("\\|");
                    if (array[0].equals("200")){
                        setShowOrGone(2);
                        Gson gson = new Gson();
                        //UserInfo user=gson.fromJson(strings[0],UserInfo.class);
                        Type typeList = new TypeToken<ArrayList<EvaluteBean>>() {
                        }.getType();
                        final ArrayList<EvaluteBean> evaluteBeen = gson.fromJson(pubClass.convertToArrayJson(result), typeList);
                        adapter=new SignEvaAdapter(getContext(), evaluteBeen);
                        mylist.setAdapter(adapter);
                        mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                EvaluteBean eb=(EvaluteBean) parent.getItemAtPosition(position);
                                if (eb.getT7().equals("暂无评估记录"))
                                {
                                    BaseActivity.showAnimaDialog(getContext(),"当前无评估记录");
                                    return;
                                }
                                Intent in = new Intent(getContext(),LookEvaDetialActivity.class);
                                in.putExtra("schid",eb.getState());
                                startActivity(in);
                            }
                        });
                    }else if (array[0].equals("201")){
                        setShowOrGone(2);
                        BaseActivity.showAnimaDialog(getContext(),"无历史数据");
                    }else{
                        setShowOrGone(2);
                        BaseActivity.showAnimaDialog(getContext(),"数据请求失败");
                    }
                }
                @Override
                public void onFailture() {
                    BaseActivity.showAnimaDialog(getContext(),"网络请求失败");
                    setShowOrGone(3);
                }
            },map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        mHasLoadedOnce = true;
    }

    public void setShowOrGone(int k){

        if (k==1){
            loadingView.setVisibility(View.VISIBLE);
            mylist.setVisibility(View.GONE);
        }
        if (k==2){
            loadingView.setVisibility(View.GONE);
            mylist.setVisibility(View.VISIBLE);
        }
        if (k==3){
            loadingView.setVisibility(View.GONE);
        }
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        setShowOrGone(1);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    loadData();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
}